package com.chatplus.application.common.config;

import cn.hutool.extra.mail.MailAccount;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * JavaMail 配置
 *
 * @author Michelle.Chung
 */
@Configuration
public class MailConfig {

    @Bean
    @ConfigurationProperties(prefix = "mail")
    public MailProperties mailProperties() {
        return new MailProperties();
    }

    @Bean
    public MailAccount mailAccount(MailProperties mailProperties) {
        MailAccount account = new MailAccount();
        account.setHost(mailProperties.getHost());
        account.setPort(mailProperties.getPort());
        account.setAuth(mailProperties.getAuth());
        account.setFrom(mailProperties.getFrom());
        account.setUser(mailProperties.getUser());
        account.setPass(mailProperties.getPass());
        account.setSocketFactoryPort(mailProperties.getPort());
        account.setStarttlsEnable(mailProperties.getStarttlsEnable());
        account.setSslEnable(mailProperties.getSslEnable());
        account.setTimeout(mailProperties.getTimeout());
        account.setConnectionTimeout(mailProperties.getConnectionTimeout());
        return account;
    }
    public static class MailProperties {
        /**
         * SMTP服务器域名
         */
        private String host;

        /**
         * SMTP服务端口
         */
        private Integer port;

        /**
         * 是否需要用户名密码验证
         */
        private Boolean auth;

        /**
         * 用户名
         */
        private String user;

        /**
         * 密码
         */
        private String pass;

        /**
         * 发送方，遵循RFC-822标准
         */
        private String from;

        /**
         * 使用 STARTTLS安全连接，STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接（TLS或SSL）， 而不是使用一个单独的加密通信端口。
         */
        private Boolean starttlsEnable;

        /**
         * 使用 SSL安全连接
         */
        private Boolean sslEnable;

        /**
         * SMTP超时时长，单位毫秒，缺省值不超时
         */
        private Long timeout;

        /**
         * Socket连接超时值，单位毫秒，缺省值不超时
         */
        private Long connectionTimeout;

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public Integer getPort() {
            return port;
        }

        public void setPort(Integer port) {
            this.port = port;
        }

        public Boolean getAuth() {
            return auth;
        }

        public void setAuth(Boolean auth) {
            this.auth = auth;
        }

        public String getUser() {
            return user;
        }

        public void setUser(String user) {
            this.user = user;
        }

        public String getPass() {
            return pass;
        }

        public void setPass(String pass) {
            this.pass = pass;
        }

        public String getFrom() {
            return from;
        }

        public void setFrom(String from) {
            this.from = from;
        }

        public Boolean getStarttlsEnable() {
            return starttlsEnable;
        }

        public void setStarttlsEnable(Boolean starttlsEnable) {
            this.starttlsEnable = starttlsEnable;
        }

        public Boolean getSslEnable() {
            return sslEnable;
        }

        public void setSslEnable(Boolean sslEnable) {
            this.sslEnable = sslEnable;
        }

        public Long getTimeout() {
            return timeout;
        }

        public void setTimeout(Long timeout) {
            this.timeout = timeout;
        }

        public Long getConnectionTimeout() {
            return connectionTimeout;
        }

        public void setConnectionTimeout(Long connectionTimeout) {
            this.connectionTimeout = connectionTimeout;
        }
    }
}
